package com.guaoran.source.mybatis.mapper;

import com.guaoran.source.mybatis.config.MapperRegistory;
import com.guaoran.source.mybatis.session.SqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author : 孤傲然
 * @Description :
 * @date :2018/5/25 23:17
 */
public class MapperProxy<T> implements InvocationHandler{
    private SqlSession sqlSession;
    private Class<T> mapperInterface;

    public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface) {
        this.sqlSession = sqlSession;
        this.mapperInterface = mapperInterface;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //System.out.println(method.getDeclaringClass().getName()+"."+method.getName());
        MapperRegistory.MapperData mapperData =
                sqlSession.
                getConfiguration().
                getMapperRegistory().
                get(method.getDeclaringClass().getName()+"."+method.getName());
        if(mapperData !=null){
            System.out.println(String.format("SQL [ %s ], parameter [%s] ", mapperData.getSql(), args[0]));
            return sqlSession.selectOne(mapperData,String.valueOf(args[0]));
        }

        return method.invoke(proxy,args);
    }
}
